Online-Academy
Look, Read, Understand, Apply

Package and Interface

Package

Package is a collection of related classes. In Java programs, The package is created to group related classes, interfaces, sub-packages. Package is a folder or directory. The name of the package and folder must be the same. The package helps to resolve the problem of naming conflict. In a Java program there can be two or more classes with the same name; in one folder we can't store two classes with the same name. To solve this problem, we can create two different packages and store those classes in different packages. Classes of one package are accessed in another package using the import statement.

  • Package is created using package keyword.
  • The package statement must be the first statement in the Java program.
  • Classes which have to be accessed in another package must be declared as public.
  • Java program stored in package is compiled staying in directory just above the package. If test is a folder and mypackage is package folder and test.java is stored in mypackage. Then test.java is compiled as follows:

    d:\test\>javac mypackage/test.java

  • import keyword is used to import package and its contents to another program.

Example: In the given example, a package named test is created; two files one.java and two.java are stored in the package test.

one.java

package test; public class one{ public int a; public String b; }

two.java

package test; public class two{ public int x; public String name; }

File three.java is stored in package named imports. In this file three.java, files one.java and two.java stored in package test is imported.

three.java

package imports; import test.one; import test.two; class three{ public static void main(String[] aaa){ one o = new one(); two t = new two(); o.a = 33; o.b = "Dineshjee"; t.x = 44; t.name = "Dinesh"; System.out.println(o.a+", "+o.b+", "+t.x+", "+t.name); } }

Interface

Interface is a collection of method prototypes (method signatures) and constant variables. Interface is created using interface keyword. An interface is implemented by other classes and the classes which implement the interface must define all the methods mentioned in the interface. A class can implement one or more interfaces. Keyword implements is used to implement an interface by a class.

    interface mathematics{
        public int sum(int a,int b);
        public int diff(int a,int b);
        public int prod(int a,int b);
    }
    
    class do_maths implements mathematics{
        public int sum(int a,int b){
            return a+b;
        }
        public int diff(int a,int b){
            return a-b;
        }
        public int prod(int a,int b){
            return a*b;
        }
    }
    class interface_demo{
        public static void main(String[] args){
            do_maths obj = new do_maths();
            System.out.println("Sum: "+obj.sum(33,55));
            System.out.println("Diff: "+obj.diff(33,55));
            System.out.println("Prod: "+obj.prod(33,55));
        }
    }